package com.se.dao.mysql;

import com.se.domain.dto.page.PageDTO;
import com.se.domain.mysql.ApplyDO;
import com.se.domain.mysql.RecordDO;
import com.se.mapper.ApplyActMapper;
import com.se.mapper.ApplyMapper;
import com.se.mapper.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ApplyDao {
    @Autowired
    ApplyMapper applyMapper;
    @Autowired
    RecordMapper recordMapper;


    public boolean existByQnId(Long qnId) {
        return applyMapper.existByQnId(qnId);
    }

    public boolean existByQnIdAndUserId(Long qnId, Long userId) {
        if(qnId == null)
            return false;

        return applyMapper.existByQnIdAndUserId(qnId, userId);
    }

    public Long getApplyIdByQnIdAndUserId(Long qnId, Long userId) {
        if(qnId == null){
            return null;
        }
        return applyMapper.getApplyIdByQnIdAndUserId(qnId, userId);
    }

    public ApplyDO getByQnIdAndUserId(Long qnId, Long userId) {
        if(qnId == null)
            return null;
        return applyMapper.getApplyByQnIdAndUserId(qnId, userId);
    }

    public Map<Long, String> mapQRecordAnsByApplyId(Long applyId) {
        List<RecordDO> recordDOList = recordMapper.listByApplyId(applyId);
        if(recordDOList.isEmpty()){
            return Collections.emptyMap();
        }

        Map<Long, String> map = new HashMap<>();
        for (RecordDO recordDO : recordDOList) {
            map.put(recordDO.getQId(), recordDO.getAns());
        }
        return map;
    }

    public ApplyDO createApply(Long userId, Long actId, Long qnId) {
        ApplyDO applyDO = new ApplyDO(userId, actId, qnId);
        applyMapper.insert(applyDO);

        return applyDO;
    }

    public void createRecords(List<RecordDO> recordDOList) {
        if(recordDOList.isEmpty()){
            return;
        }
        recordMapper.insertAll(recordDOList);
    }

    public ApplyDO getByApplyId(Long applyId) {
        return applyMapper.getByApplyId(applyId);
    }

    public void cancelApply(Long applyId) {
        applyMapper.logicDeleteByApplyId(applyId);
    }

    public void signByApplyId(Long applyId) {
        applyMapper.updateSignByApplyId(applyId, true);
    }

    public List<ApplyDO> listUserApplyByQnId(Long qnId, PageDTO page) {
        if(qnId == null){
            return Collections.emptyList();
        }
        return applyMapper.listByAnId(qnId, page.limitStart(), page.getSize());
    }

    public List<RecordDO> listRecordsByQId(Long qId) {
        if(qId == null){
            return Collections.emptyList();
        }

        return recordMapper.listByQId(qId);
    }


    public List<Long> listUserIdByQnId(Long qnId) {
        if(qnId == null){
            return Collections.emptyList();
        }
        return applyMapper.listUserIdByQnId(qnId);
    }

    public List<ApplyDO> listUserApplyByQnId(Long qnId) {
        if(qnId == null){
            return Collections.emptyList();
        }
        return applyMapper.listByQnId(qnId);
    }
}
